"use strict";
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
const taro = require("../../taro.js");
const common = require("../../common.js");
const index_vue_vue_type_style_index_0_lang = "";
const _sfc_main = {
  name: "LeaderboardPage",
  setup() {
    const currentTab = taro.ref("score");
    const leaderboardData = taro.ref([]);
    const myRank = taro.ref(null);
    const loading = taro.ref(false);
    const hasMore = taro.ref(true);
    const currentPage = taro.ref(1);
    const tabs = [
      { key: "score", label: "总分" },
      { key: "time", label: "速度" },
      { key: "level", label: "等级" }
    ];
    const currentTabLabel = taro.computed(() => {
      const tab = tabs.find((t) => t.key === currentTab.value);
      return tab ? tab.label : "总分";
    });
    const switchTab = (tabKey) => {
      if (currentTab.value === tabKey)
        return;
      currentTab.value = tabKey;
      currentPage.value = 1;
      hasMore.value = true;
      loadLeaderboardData(true);
    };
    const loadLeaderboardData = (reset = false) => __async(this, null, function* () {
      var _a, _b;
      if (loading.value)
        return;
      try {
        loading.value = true;
        const page = reset ? 1 : currentPage.value;
        const response = yield common.leaderboardAPI.getLeaderboard(currentTab.value, page, 20);
        const data = response.leaderboard || ((_a = response.data) == null ? void 0 : _a.leaderboard) || response || [];
        if (reset) {
          leaderboardData.value = data;
        } else {
          leaderboardData.value.push(...data);
        }
        const pagination = response.pagination || ((_b = response.data) == null ? void 0 : _b.pagination);
        hasMore.value = pagination ? pagination.page < pagination.pages : data.length === 20;
        currentPage.value = page + 1;
      } catch (error) {
        console.error("加载排行榜数据失败:", error);
        common.handleAPIError(error);
      } finally {
        loading.value = false;
      }
    });
    const refreshData = () => {
      loadLeaderboardData(true);
    };
    const loadMore = () => {
      if (hasMore.value && !loading.value) {
        loadLeaderboardData(false);
      }
    };
    const formatScore = (score) => {
      if (typeof score !== "number")
        return "0";
      return score.toLocaleString();
    };
    const formatTime = (seconds) => {
      if (typeof seconds !== "number")
        return "00:00";
      const minutes = Math.floor(seconds / 60);
      const secs = seconds % 60;
      return `${minutes.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}`;
    };
    const getMedalEmoji = (index) => {
      const medals = ["🥇", "🥈", "🥉"];
      return medals[index] || "";
    };
    const getSubInfo = (item) => {
      switch (currentTab.value) {
        case "score":
          return `最佳: ${formatTime(item.bestTime || 0)}`;
        case "time":
          return `平均: ${formatTime(item.avgTime || 0)}`;
        case "level":
          return `经验: ${item.experience || 0}`;
        default:
          return "";
      }
    };
    taro.onMounted(() => {
      loadLeaderboardData(true);
    });
    return {
      // 数据
      currentTab,
      leaderboardData,
      myRank,
      loading,
      hasMore,
      tabs,
      // 计算属性
      currentTabLabel,
      // 方法
      switchTab,
      loadLeaderboardData,
      refreshData,
      loadMore,
      formatScore,
      formatTime,
      getMedalEmoji,
      getSubInfo
    };
  }
};
const _hoisted_1 = { class: "leaderboard-page" };
const _hoisted_2 = { class: "page-header" };
const _hoisted_3 = { class: "header-tabs" };
const _hoisted_4 = ["onTap"];
const _hoisted_5 = { class: "tab-text" };
const _hoisted_6 = { class: "leaderboard-list" };
const _hoisted_7 = { class: "list-header" };
const _hoisted_8 = { class: "list-title" };
const _hoisted_9 = {
  key: 0,
  class: "loading-container"
};
const _hoisted_10 = { class: "rank-list" };
const _hoisted_11 = { class: "rank-left" };
const _hoisted_12 = {
  key: 0,
  class: "rank-medal"
};
const _hoisted_13 = { class: "medal-text" };
const _hoisted_14 = {
  key: 1,
  class: "rank-num"
};
const _hoisted_15 = { class: "user-avatar" };
const _hoisted_16 = { class: "avatar-text" };
const _hoisted_17 = { class: "user-details" };
const _hoisted_18 = { class: "user-name" };
const _hoisted_19 = { class: "user-level" };
const _hoisted_20 = { class: "rank-right" };
const _hoisted_21 = { class: "score-info" };
const _hoisted_22 = { class: "main-score" };
const _hoisted_23 = { class: "sub-info" };
const _hoisted_24 = {
  key: 0,
  class: "empty-state"
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return taro.openBlock(), taro.createElementBlock("view", _hoisted_1, [
    taro.createCommentVNode(" 页面头部 "),
    taro.createBaseVNode("view", _hoisted_2, [
      _cache[2] || (_cache[2] = taro.createBaseVNode(
        "view",
        { class: "header-title" },
        [
          taro.createBaseVNode("text", { class: "title-text" }, "排行榜")
        ],
        -1
        /* CACHED */
      )),
      taro.createBaseVNode("view", _hoisted_3, [
        (taro.openBlock(true), taro.createElementBlock(
          taro.Fragment,
          null,
          taro.renderList($setup.tabs, (tab) => {
            return taro.openBlock(), taro.createElementBlock("view", {
              key: tab.key,
              class: taro.normalizeClass(["tab-item", { active: $setup.currentTab === tab.key }]),
              onTap: ($event) => $setup.switchTab(tab.key)
            }, [
              taro.createBaseVNode(
                "text",
                _hoisted_5,
                taro.toDisplayString(tab.label),
                1
                /* TEXT */
              )
            ], 42, _hoisted_4);
          }),
          128
          /* KEYED_FRAGMENT */
        ))
      ])
    ]),
    taro.createCommentVNode(" 个人排名功能已移除 "),
    taro.createCommentVNode(" 排行榜列表 "),
    taro.createBaseVNode("view", _hoisted_6, [
      taro.createBaseVNode("view", _hoisted_7, [
        taro.createBaseVNode(
          "text",
          _hoisted_8,
          taro.toDisplayString($setup.currentTabLabel) + "排行榜",
          1
          /* TEXT */
        ),
        taro.createBaseVNode(
          "view",
          {
            class: "refresh-btn",
            onTap: _cache[0] || (_cache[0] = (...args) => $setup.refreshData && $setup.refreshData(...args))
          },
          _cache[3] || (_cache[3] = [
            taro.createBaseVNode(
              "text",
              { class: "refresh-text" },
              "刷新",
              -1
              /* CACHED */
            )
          ]),
          32
          /* NEED_HYDRATION */
        )
      ]),
      taro.createCommentVNode(" 加载状态 "),
      $setup.loading ? (taro.openBlock(), taro.createElementBlock("view", _hoisted_9, _cache[4] || (_cache[4] = [
        taro.createBaseVNode(
          "text",
          { class: "loading-text" },
          "加载中...",
          -1
          /* CACHED */
        )
      ]))) : (taro.openBlock(), taro.createElementBlock(
        taro.Fragment,
        { key: 1 },
        [
          taro.createCommentVNode(" 排行榜项目 "),
          taro.createBaseVNode("view", _hoisted_10, [
            (taro.openBlock(true), taro.createElementBlock(
              taro.Fragment,
              null,
              taro.renderList($setup.leaderboardData, (item, index) => {
                return taro.openBlock(), taro.createElementBlock(
                  "view",
                  {
                    key: item.id || index,
                    class: taro.normalizeClass(["rank-item", {
                      "rank-top1": index === 0,
                      "rank-top3": index < 3,
                      "rank-me": item.isMe
                    }])
                  },
                  [
                    taro.createBaseVNode("view", _hoisted_11, [
                      index < 3 ? (taro.openBlock(), taro.createElementBlock("view", _hoisted_12, [
                        taro.createBaseVNode(
                          "text",
                          _hoisted_13,
                          taro.toDisplayString($setup.getMedalEmoji(index)),
                          1
                          /* TEXT */
                        )
                      ])) : (taro.openBlock(), taro.createElementBlock(
                        "text",
                        _hoisted_14,
                        taro.toDisplayString(index + 1),
                        1
                        /* TEXT */
                      )),
                      taro.createBaseVNode("view", _hoisted_15, [
                        taro.createBaseVNode(
                          "text",
                          _hoisted_16,
                          taro.toDisplayString((item.username || "玩家").charAt(0).toUpperCase()),
                          1
                          /* TEXT */
                        )
                      ]),
                      taro.createBaseVNode("view", _hoisted_17, [
                        taro.createBaseVNode(
                          "text",
                          _hoisted_18,
                          taro.toDisplayString(item.username || `玩家${item.id}`),
                          1
                          /* TEXT */
                        ),
                        taro.createBaseVNode(
                          "text",
                          _hoisted_19,
                          "Lv." + taro.toDisplayString(item.level || 1),
                          1
                          /* TEXT */
                        )
                      ])
                    ]),
                    taro.createBaseVNode("view", _hoisted_20, [
                      taro.createBaseVNode("view", _hoisted_21, [
                        taro.createBaseVNode(
                          "text",
                          _hoisted_22,
                          taro.toDisplayString($setup.formatScore(item.score)),
                          1
                          /* TEXT */
                        ),
                        taro.createBaseVNode(
                          "text",
                          _hoisted_23,
                          taro.toDisplayString($setup.getSubInfo(item)),
                          1
                          /* TEXT */
                        )
                      ])
                    ])
                  ],
                  2
                  /* CLASS */
                );
              }),
              128
              /* KEYED_FRAGMENT */
            )),
            taro.createCommentVNode(" 空状态 "),
            $setup.leaderboardData.length === 0 ? (taro.openBlock(), taro.createElementBlock("view", _hoisted_24, _cache[5] || (_cache[5] = [
              taro.createBaseVNode(
                "text",
                { class: "empty-text" },
                "暂无排行数据",
                -1
                /* CACHED */
              )
            ]))) : taro.createCommentVNode("v-if", true)
          ])
        ],
        2112
        /* STABLE_FRAGMENT, DEV_ROOT_FRAGMENT */
      ))
    ]),
    taro.createCommentVNode(" 底部加载更多 "),
    $setup.hasMore && !$setup.loading ? (taro.openBlock(), taro.createElementBlock(
      "view",
      {
        key: 0,
        class: "load-more",
        onTap: _cache[1] || (_cache[1] = (...args) => $setup.loadMore && $setup.loadMore(...args))
      },
      _cache[6] || (_cache[6] = [
        taro.createBaseVNode(
          "text",
          { class: "load-more-text" },
          "加载更多",
          -1
          /* CACHED */
        )
      ]),
      32
      /* NEED_HYDRATION */
    )) : taro.createCommentVNode("v-if", true)
  ]);
}
const component = /* @__PURE__ */ common._export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "D:/code/数独/sudoku-game/src/pages/leaderboard/index.vue"]]);
var config = {
  "navigationBarTitleText": "排行榜",
  "navigationBarBackgroundColor": "#667eea",
  "navigationBarTextStyle": "white"
};
Page(taro.createPageConfig(component, "pages/leaderboard/index", { root: { cn: [] } }, config || {}));
